package ink.yql.service.Impl;

import ink.yql.dao.AddressBookDao;
import ink.yql.dao.UserDao;
import ink.yql.model.AddressBook;
import ink.yql.model.User;
import ink.yql.service.AddressBookService;
import ink.yql.util.Check;
import ink.yql.util.RestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author YQL
 */
@Service
@Transactional
public class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    private AddressBookDao addressBookDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private HttpServletRequest req;

    @Override
    public RestObject getByUidDefault() {
        // 获取登录用户
        User user = (User) req.getSession().getAttribute("user");
        // 判断是否登录
        if (user == null || user.getUid() == null) {
            return RestObject.builder().code("404").message("请先登录").build();
        }
        // 获取登录用户的默认地址
        AddressBook addressBook = addressBookDao.getByUidDefault(user.getUid());
        // 返回数据
        return RestObject.builder().code("200").message("查询成功").data(addressBook).build();
    }

    @Override
    public RestObject getByUid() {
        // 获取登录用户
        User user = (User) req.getSession().getAttribute("user");
        // 判断是否登录
        if (user == null || user.getUid() == null) {
            return RestObject.builder().code("404").message("请先登录").build();
        }
        // 获取登录用户的所有地址
        List<AddressBook> list = addressBookDao.getByUid(user.getUid());
        // 返回数据
        return RestObject.builder().code("200").message("查询成功").data(list).build();
    }

    @Override
    public RestObject addOne(AddressBook addressBook) {
        try {
            // 获取登录的uid
            User user = (User) req.getSession().getAttribute("user");
            if (user == null) {
                return RestObject.builder().code("404").message("请先登录").build();
            }
            // 设置给地址
            addressBook.setUid(user.getUid());
            // 校验地址信息是否为空
            if (!Check.addressBook(addressBook)) {
                return RestObject.builder().code("400").message("输入的数据不全").build();
            }
            // 如果设置默认值重置默认值
            if (addressBook.getAddefault() == 1) {
                AddressBook aDefault = addressBookDao.getByUidDefault(user.getUid());
                if (aDefault != null) {
                    aDefault.setAddefault(0);
                    addressBookDao.updateByAdid(aDefault);
                }

            }
            // 添加
            addressBookDao.addOne(addressBook);
            // 返回响应数据
            return RestObject.builder().code("200").message("添加成功").build();
        } catch (Exception e) {
            // 如有报错响应数据
            return RestObject.builder().code("400").message("输入的数据不全").build();
        }
    }

    @Override
    public RestObject updateOne(AddressBook addressBook) {
        // 获取登录的uid
        User user = (User) req.getSession().getAttribute("user");
        if (user == null) {
            return RestObject.builder().code("404").message("请先登录").build();
        }
        // 设置给地址
        addressBook.setUid(user.getUid());
        // 校验地址信息是否为空
        if (!Check.addressBook(addressBook)) {
            return RestObject.builder().code("400").message("输入的数据不全").build();
        }
        AddressBook byAbid = addressBookDao.getByAbid(addressBook.getAbid());
        if (byAbid.equals(addressBook)) {
            return RestObject.builder().code("200").message("地址修改成功").build();
        }
        if (byAbid.getAddefault() == 1) {
            AddressBook aDefault = addressBookDao.getByUidDefault(user.getUid());
            if (aDefault != null) {
                aDefault.setAddefault(0);
                addressBookDao.updateByAdid(aDefault);
            }
        }
        addressBookDao.updateByAdid(addressBook);
        return RestObject.builder().code("200").message("地址修改成功").build();
    }

    @Override
    public RestObject deleteByAbid(Integer abid) {
        // 校验参数
        if (abid == null) {
            return RestObject.builder().code("400").message("请一个正确的参数").build();
        }
        // 删除
        Integer delete = addressBookDao.deleteByAbid(abid);
        // 如果没有
        if (delete == 0) {
            return RestObject.builder().code("400").message("请一个正确的参数").build();
        }
        // 如果有
        return RestObject.builder().code("200").message("删除成功").build();
    }

    @Override
    public RestObject getByAbid(Integer abid) {
        // 校验参数
        if (abid == null) {
            return RestObject.builder().code("400").message("请一个正确的参数").build();
        }
        // 获取地址
        AddressBook addressBook = addressBookDao.getByAbid(abid);
        // 返回数据
        return RestObject.builder().code("200").message("查询成功").data(addressBook).build();

    }

}
